package com.zqc.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqc.common.dto.SysMenuDto;
import com.zqc.entity.SysMenu;
import com.zqc.entity.SysUser;
import com.zqc.mapper.SysMenuMapper;
import com.zqc.mapper.SysUserMapper;
import com.zqc.service.SysMenuService;
import com.zqc.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zqc
 * @since 2023-03-07
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    public SysUserService sysUserService;

    @Autowired
    public SysUserMapper sysUserMapper;

    @Override
    public List<SysMenuDto> getCurrentUserNav() {
        List<SysMenuDto> sysMenuDtos = new ArrayList<SysMenuDto>();

        // 获取当前用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // 获取当前用户
        SysUser user = sysUserService.getUserByUsername(username);

        // 根据用户获取用户所拥有的菜单的ids
        List<Long> navMenuIds = sysUserMapper.getNavMenuIds(user.getId());
        // 如果菜单id为空，直接返回
        if (navMenuIds.size() == 0) {
            return null;
        }
        // 根据菜单id获取全部菜单信息
        List<SysMenu> sysMenus = this.listByIds(navMenuIds);

        // 将获取的菜单信息转成
        List<SysMenu> treeMenus = TreeMenu(sysMenus);

        // 将树状菜单集合转成前端需要的数据类型
        sysMenuDtos = convert(treeMenus);

        return sysMenuDtos;
    }

    // 查询所有菜单信息,返回树状数据
    @Override
    public List<SysMenu> getTreeMenu() {
        List<SysMenu> menuList = this.list(new QueryWrapper<SysMenu>().orderByAsc("orderNum"));
        // 将获取的菜单信息转成树状数据
        List<SysMenu> treeMenu = TreeMenu(menuList);
        return treeMenu;
    }


    private List<SysMenu> TreeMenu(List<SysMenu> sysMenus) {
        List<SysMenu> treeMenus = new ArrayList<>();
        // 找到所有菜单所拥有的的子菜单
        for (SysMenu menu : sysMenus) {
            for (SysMenu e : sysMenus) {
                if (menu.getId() == e.getParentId()) {
                    menu.getChildren().add(e);
                }
            }
            // 当前菜单为顶级菜单
            if (menu.getParentId() == 0L) {
                treeMenus.add(menu);
            }
        }
        System.out.println(JSONUtil.toJsonStr(treeMenus));
        return treeMenus;
    }

    private List<SysMenuDto> convert(List<SysMenu> treeMenus) {
        List<SysMenuDto> treeMenuDtos = new ArrayList<>();

        treeMenus.forEach(m -> {
            SysMenuDto sysMenuDto = new SysMenuDto();
            sysMenuDto.setId(m.getId());
            sysMenuDto.setName(m.getPerms());
            sysMenuDto.setTitle(m.getName());
            sysMenuDto.setIcon(m.getIcon());
            sysMenuDto.setComponent(m.getComponent());
            sysMenuDto.setPath(m.getMenuPath());

            if (m.getChildren().size() > 0) {
                // 递归调用
                sysMenuDto.setChildren(convert(m.getChildren()));
            }

            treeMenuDtos.add(sysMenuDto);
        });

        return treeMenuDtos;
    }
}
